package com.cunzai.blog.application.article.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cunzai.blog.application.article.dto.ArticlePageResponse;
import com.cunzai.blog.application.article.service.ArticleService;
import com.cunzai.blog.common.PO.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

// ArticleController.java
@RestController
@RequestMapping("/api/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping
    public ResponseEntity<?> getArticles(@RequestParam(required = false) Long category,
                                         @RequestParam(required = false) Long tag,
                                         @RequestParam(defaultValue = "1") int page) {
        Page<Article> articles = articleService.getArticles(category, tag, page);
        return ResponseEntity.ok(new ArticlePageResponse(articles));
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getArticle(@PathVariable Long id) {
        Article article = articleService.getArticleById(id);
        return ResponseEntity.ok(article);
    }

    // 其他 CRUD 操作

    @GetMapping("/hello")
    public ResponseEntity<String> helloWorld() {
        return ResponseEntity.ok("Hello, World! This is a test endpoint.");
    }
}
